INCLUDEFLAGS = -I.

ifeq ($(generic), true)
	CFLAGS = $(INCLUDEFLAGS) -Ofast -DSEIR_RAND
else
	CFLAGS = $(INCLUDEFLAGS) -Ofast -DSEIR_RAND -march=native
endif

LDFLAGS = -lm -lpthread

DEPS = $(shell $(CC) $(INCLUDEFLAGS) -MM -MT $(1).o $(1).c | sed -z 's/\\\n //g')

.PHONY: all clean cleanall release
all: bin bin/fat bin/fatserver

bin:
	mkdir bin

$(call DEPS,client/main)
	$(CC) $(CFLAGS) -c $< -o $@

$(call DEPS,client/glad_gl)
	$(CC) $(CFLAGS) -c $< -o $@

# processing exo.c is too slow, hardcode
#$(call DEPS,assets/exo)
#	$(CC) $(CFLAGS) -c $< -o $@
assets/exo.o: assets/exo.c assets/exo.h
	$(CC) $(CFLAGS) -c $< -o $@

$(call DEPS,inc/vec)
	$(CC) $(CFLAGS) -c $< -o $@

#$(call DEPS,inc/protocol)
#	$(CC) $(CFLAGS) -c $< -o $@


bin/fat: client/main.o client/glad_gl.o assets/exo.o inc/vec.o
	$(CC) $^ $(LDFLAGS) -lglfw -o $@


$(call DEPS,server/main)
	$(CC) $(CFLAGS) -c $< -o $@

$(call DEPS,server/utils)
	$(CC) $(CFLAGS) -c $< -o $@


bin/fatserver: server/main.o server/utils.o assets/exo.o inc/vec.o client/glad_gl.o
	$(CC) $^ $(LDFLAGS) -o $@


run: bin/fat
	bin/fat

clean:
	$(RM) bin/fat bin/fatserver client/*.o server/*.o inc/*.o bin/*.upx

cleanall: clean
	$(RM) assets/*.o

release: bin/fat
	upx --lzma --best bin/fat
